/* Data analysis of Follow-up Data for Nepal CCF: Household */

local folder "C:\Users\mlevere\OneDrive - Mathematica\Documents\Projects\Nepal\"

/* Merge together all the sections into one big dataset 
   Rename all the variables along the way */
use "`folder'/Data/FollowUp_Raw/SEC01", clear
rename q01_101 round_num
rename q01_102 co_name
rename q01_103 co_code
rename q01_104 house_sn
rename q01_104sn house_sn_sub
rename q01_105a num_elig_women
rename q01_105b int_elig_women
rename q01_106 co_mem_name
rename q01_107dd interview_day
rename q01_107mm interview_month
rename q01_107yy interview_year
rename q01_108 interviewer_name
rename q01_109 supervisor_name
rename q01_110 editor_name
rename q01_111 dataentry_name
tempfile follow_up_raw_hh
save "`follow_up_raw_hh'", replace

merge 1:1 www hh sn using "`folder'/Data/FollowUp_Raw/SEC02", nogen
rename q02_201 district
rename q02_202 vdc
rename q02_203a vdc_name
rename q02_203b vdc_code
rename q02_204 ward_num
rename q02_205 village_name
rename q02_206a1 main_respondent_name
rename q02_206a2 main_respondent_id
rename q02_206b1 second_respondent_name
rename q02_206b2 second_respondent_id
rename q02_207a hh_head_name
rename q02_207b hh_head_id
rename q02_208 hh_head_caste
rename q02_209 spouse_name
rename q02_210 houseleft_name
rename q02_211 houseright_name
rename q02_212 survey_result
rename q02_213 religion
rename q02_214 tot_family
save "`follow_up_raw_hh'", replace

use "`folder'/Data/FollowUp_Raw/SEC03", clear
rename q03_idc id_code
rename q03_301 name
rename q03_302 relationship
rename q03_303 sex
rename q03_304a age_years
rename q03_304b age_months
rename q03_305 literacy
rename q03_306 school
rename q03_307 highest_grade
rename q03_308 marital_status
rename q03_309a occupation_main
rename q03_309b occupation_secondary
rename q03_310 residing
rename q03_311 eligible
by www hh sn, sort: egen num_hh_members_raw = count(id_code)
by www hh sn, sort: egen num_hh_members_residing_raw = count(id_code) if residing == 1
by www hh sn, sort: egen num_eligible_women_raw = count(id_code) if eligible == 1 & age_year > 5
by www hh sn, sort: egen num_eligible_kids_raw = count(id_code) if eligible == 1 & age_year <= 5
by www hh sn, sort: egen num_hh_members = max(num_hh_members_raw)
by www hh sn, sort: egen num_hh_members_residing = max(num_hh_members_residing_raw)
by www hh sn, sort: egen num_eligible_women = max(num_eligible_women_raw)
by www hh sn, sort: egen num_eligible_kids = max(num_eligible_kids_raw)
keep if id_code == 1
gen hh_head_male = sex == 1
gen hh_head_age = age_years
keep www hh sn num_hh_members num_hh_members_residing num_eligible_women num_eligible_kids hh_head_male hh_head_age
tempfile SEC03_hh
save "`SEC03_hh'", replace
merge 1:1 www hh sn using "`follow_up_raw_hh'", nogen
save "`follow_up_raw_hh'", replace

use "`follow_up_raw_hh'", clear
merge 1:1 www hh sn using "`folder'/Data/FollowUp_Raw/SEC04", nogen
rename q04_401 hh_occupy_only
rename q04_402 ownership
rename q04_403 flooring
rename q04_404 outside_wall
rename q04_405 roof
rename q04_406a fuel_main
rename q04_406b fuel_second
rename q04_406c fuel_third
rename q04_407 toilet
rename q04_408 own_land
rename q04_409 own_livestock
save "`follow_up_raw_hh'", replace

use "`folder'/Data/FollowUp_Raw/SEC05A", clear
drop if q05_500a == .
reshape wide q05_500b1 q05_500b2, i(www hh sn) j(q05_500a)
local foods "rice beatenrice maize wheat barley millet pulses pea othbeans fish meat egg milk curd powdermilk ghee honey vegghee oil potato onion greenveg othveg fruitsnuts salt othspice sugar sakhar sweets tea alcohol nonalcohol tobacco noodles othfoods"
local i = 1
local n : word count `foods'
while `i' < = `n' {
local change : word `i' of `foods'
rename q05_500b1`i' food_`change'_amt
rename q05_500b2`i' food_`change'_unit
label variable food_`change'_amt "Food recall: Quantity of `change'"
label variable food_`change'_unit "Food recall: Unit of `change'"
local i = `i' + 1
}
tempfile SEC05A_wide
save "`SEC05A_wide'", replace

use "`follow_up_raw_hh'", clear
merge 1:1 www hh sn using "`SEC05A_wide'", nogen
save "`follow_up_raw_hh'", replace

merge 1:1 www hh sn using "`folder'/Data/FollowUp_Raw/SEC05B", nogen
rename q05_501 eq_property_damage
rename q05_502 eq_home_destroyed
rename q05_503 eq_displaced
rename q05_504 eq_displaced_days
rename q05_505 eq_family_death
rename q05_506 eq_village_death
rename q05_507 eq_aid
rename q05_508a eq_aid_local
rename q05_508b eq_aid_natl
rename q05_508c eq_aid_intl
rename q05_509 eq_water
rename q05_510 eq_food
rename q05_511 eq_assistance
rename q05_512a eq_assistance_health
rename q05_512b eq_assistance_food
rename q05_512c eq_assistance_shelter
rename q05_512d eq_assistance_labor
rename q05_512e eq_assistance_money
rename q05_512f eq_assistance_training
rename q05_512g eq_assistance_other
rename q05_513 eq_remittances
rename q05_514 eq_remittances_amt
rename q05_515 eq_hh_pregnant
rename q05_516 eq_hh_pregnant_months
rename q05_517 eq_hh_pregnant_complete
rename q05_518 eq_hh_pregnant_complete_months
save "`follow_up_raw_hh'", replace

// Calories per person

/***********************************/
/* STEP 1: CONVERT FOOD INTO GRAMS */
/***********************************/

/* Food Conversion Factors:
   1 Mana = 0.545 liters
   1 kuruwa = 2 mana = 1.09 liters
   1 pathi = 8 mana = 4.36 liters
   1 Spoon = 5.92 ml */
   
/* 	CONVERSIONS OF VOLUME TO GRAMS (per CEDA)
    replace grams_manna=452 RICE
	replace grams_manna=276 BEATEN RICE
	replace grams_manna=395 MAIZE
	replace grams_manna=281 MAIZE FLOUR
	replace grams_manna=281 WHEAT FLOUR
	replace grams_manna=454 MILLET
	replace grams_manna=444 BLACK PULSE
	replace grams_manna=432 MASOOR (AVERAGE = 438 -> OTHER BEANS)
	replace grams_manna=443 RAHAR (PULSES)
	replace grams_manna=458 GRAM (GRAM/PEA)
	replace grams_manna=568 MILK
	replace grams_manna=514 CURD
	replace grams_manna=494 GHEE
	replace grams_manna=538 OIL
	replace grams_manna=375 POTATOES
	replace grams_manna=500 SALT
	NOTE: STILL NEED HONEY, ONION, OTHER SPICES, SUGAR, TEA, NOODLES */
	
/* CONVERSIONS OF NUMBER TO GRAMS (per weighing)
   replace grams_number = 200 POTATOES
   replace grams_number = 175 ONIONS
   replace grams_number = 750 GREEN LEAFY VEGETABLES
   replace grams_number = 500 OTHER VEGETABLES (CABBAGE AND BROCCOLI)
   replace grams_number = 30 BANANA (FOR UNIT DOZENS IN FRUITS/NUTS)
   replace grams_number = 250 FRUITS/NUTS (APPLE AND GUAVA, APPROXIMATELY IN MIDDLE OF FRUITS WEIGHED) */


/* Convert rice to grams as follows:
   1 mana = 452 grams */
   
drop if food_rice_amt == .
   
gen food_rice_grams = food_rice_amt
replace food_rice_grams = food_rice_amt * 1000 if food_rice_unit == 1
replace food_rice_grams = food_rice_amt * 452 * 8 if food_rice_unit == 5
replace food_rice_grams = food_rice_amt * 452 if food_rice_unit == 6
replace food_rice_grams = food_rice_amt * 452 * 2 if food_rice_unit == 7

/* Convert beaten rice to grams as follows:
   1 mana = 276 grams  */
   
gen food_beatenrice_grams = food_beatenrice_amt
replace food_beatenrice_grams = food_beatenrice_amt * 1000 if food_beatenrice_unit == 1
replace food_beatenrice_grams = food_beatenrice_amt * 276 if food_beatenrice_unit == 6
replace food_beatenrice_grams = food_beatenrice_amt * 276 * 2 if food_beatenrice_unit == 7

/* Convert maize to grams as follows:
   1 number (ear) = 89.5 grams
   1 mana = 395 grams */
gen food_maize_grams = food_maize_amt
replace food_maize_grams = food_maize_amt * 1000 if food_maize_unit == 1
replace food_maize_grams = food_maize_amt * 395 * 8 if food_maize_unit == 5
replace food_maize_grams = food_maize_amt * 395 if food_maize_unit == 6
replace food_maize_grams = food_maize_amt * 395 * 2 if food_maize_unit == 7
replace food_maize_grams = food_maize_amt * 89.5 if food_maize_unit == 8

/* Convert wheat to grams as follows:
   1 mana = 281 grams */
gen food_wheat_grams = food_wheat_amt
replace food_wheat_grams = food_wheat_amt * 1000 if food_wheat_unit == 1
replace food_wheat_grams = 281 * 8 if food_wheat_unit == 5
replace food_wheat_grams = 281 if food_wheat_unit == 6
replace food_wheat_grams = 281 * 2 if food_wheat_unit == 7

/* Convert barley to grams as follows:
   1 mana = 2.30358 cups = 183.8983 * 2.30358 = 424 grams (see here: http://www.fatsecret.com/calories-nutrition/usda/barley) */
gen food_barley_grams = food_barley_amt
replace food_barley_grams = food_barley_amt * 1000 if food_barley_unit == 1
replace food_barley_grams = food_barley_amt * 424 if food_barley_unit == 6

/* Convert millet to grams as follows:
   1 mana = 454 grams */
gen food_millet_grams = food_millet_amt
replace food_millet_grams = food_millet_amt * 1000 if food_millet_unit == 1
replace food_millet_grams = food_millet_amt * 454 * 8 if food_millet_unit == 5
replace food_millet_grams = food_millet_amt * 454 if food_millet_unit == 6


/* Convert pulses to grams as follows:
   1 mana = 443 grams */
gen food_pulses_grams = food_pulses_amt
replace food_pulses_grams = food_pulses_amt * 1000 if food_pulses_unit == 1
replace food_pulses_grams = food_pulses_amt * 443 if food_pulses_unit == 6
replace food_pulses_grams = food_pulses_amt * 443 * 2 if food_pulses_unit == 7

/* Convert pea to grams as follows:
   1 mana = 458 grams */
gen food_pea_grams = food_pea_amt
replace food_pea_grams = food_pea_amt * 1000 if food_pea_unit == 1
replace food_pea_grams = food_pea_amt * 458 if food_pea_unit == 6
replace food_pea_grams = food_pea_amt * 458 * 2 if food_pea_unit == 7


/* Convert othbeans to grams as follows:
   1 mana = 438 grams */
gen food_othbeans_grams = food_othbeans_amt
replace food_othbeans_grams = food_othbeans_amt * 1000 if food_othbeans_unit == 1
replace food_othbeans_grams = food_othbeans_amt * 438 if food_othbeans_unit == 6

/* Convert fish to grams
   NOTE: 1 weird outlier: 1 gram */
gen food_fish_grams = food_fish_amt
replace food_fish_grams = food_fish_amt * 1000 if food_fish_unit == 1

/* Convert meat to grams
   NOTE: 2 weird outliers: 12 grams, 0.5 grams */
gen food_meat_grams = food_meat_amt
replace food_meat_grams = food_meat_amt * 1000 if food_meat_unit == 1

/* Convert eggs to number */
gen food_egg_num = food_egg_amt
replace food_egg_num = food_egg_amt * 12 if food_egg_unit == 9

/* Convert milk to liters
   NOTE: 41 people say less than 10 ml, these should probably be liters */
gen food_milk_liter = food_milk_amt
replace food_milk_liter = food_milk_amt/1000 if food_milk_unit == 4
replace food_milk_liter = food_milk_amt * 0.545 * 8 if food_milk_unit == 5
replace food_milk_liter = food_milk_amt * 0.545 if food_milk_unit == 6
replace food_milk_liter = food_milk_amt * 0.545 * 2 if food_milk_unit == 7

/* Convert curd to grams
   Some people use ml/liters, some use pathi/mana/kuruwa
   1 mana = 514 grams */
gen food_curd_grams = food_curd_amt
replace food_curd_grams = food_curd_amt * 1000 if food_curd_unit == 1
replace food_curd_grams = food_curd_amt * 514 / 0.545 if food_curd_unit == 3
replace food_curd_grams = food_curd_amt * 514 / 545 if food_curd_unit == 4
replace food_curd_grams = food_curd_amt * 514 * 8 if food_curd_unit == 5
replace food_curd_grams = food_curd_amt * 514 if food_curd_unit == 6
replace food_curd_grams = food_curd_amt * 514 * 2 if food_curd_unit == 7

/* Convert powder milk to grams */
gen food_powdermilk_grams = food_powdermilk_amt
replace food_powdermilk_grams = food_powdermilk_amt * 1000 if food_powdermilk_unit == 1
replace food_powdermilk_grams = food_powdermilk_amt * 500 if food_powdermilk_unit == 8

/* Convert ghee to grams  as follows:
   1 mana = 494 grams */
gen food_ghee_grams = food_ghee_amt
replace food_ghee_grams = food_ghee_amt * 1000 if food_ghee_unit == 1
replace food_ghee_grams = food_ghee_amt * 494 / 545 if food_ghee_unit == 4
replace food_ghee_grams = food_ghee_amt * 494 if food_ghee_unit == 6
replace food_ghee_grams = food_ghee_amt * 5.92 * 494 / 545 if food_ghee_unit == 10

/* Convert honey to grams  as follows:
   see here: http://www.fatsecret.com/calories-nutrition/usda/honey
   1 teaspoon = 7.01754 grams
   1 ml = 0.202884 tsp = 7.01754*0.202884 grams = 1.424 grams
   -> 1 mana = 776 grams */
gen food_honey_grams = food_honey_amt
replace food_honey_grams = food_honey_amt * 776 / 545 if food_honey_unit == 4
replace food_honey_grams = food_honey_amt * 5.92 * 776 / 545 if food_honey_unit == 10

/* Convert vegetable ghee to grams  as follows:
   1 mana = 494 grams */
gen food_vegghee_grams = food_vegghee_amt
replace food_vegghee_grams = food_vegghee_amt * 494 / 0.545 if food_vegghee_unit == 4
replace food_vegghee_grams = food_vegghee_amt * 494 / 545 if food_vegghee_unit == 4
replace food_vegghee_grams = food_vegghee_amt * 5.92 * 494 / 545 if food_vegghee_unit == 10

/* Convert oil to grams
   1 mana = 538 grams */
gen food_oil_grams = food_oil_amt
replace food_oil_grams = food_oil_amt * 1000 if food_oil_unit == 1
replace food_oil_grams = food_oil_amt * 538 / 0.545 if food_oil_unit == 3
replace food_oil_grams = food_oil_amt * 538 / 545 if food_oil_unit == 4
replace food_oil_grams = food_oil_amt * 538 if food_oil_unit == 6
replace food_oil_grams = food_oil_amt * 5.92 * 538 / 545 if food_oil_unit == 10

/* Convert potato to grams
   1 mana = 375 grams
   17 observations with < 10 grams, seems like miscoded units */
gen food_potato_grams = food_potato_amt
replace food_potato_grams = food_potato_amt * 1000 if food_potato_unit == 1
replace food_potato_grams = food_potato_amt * 375 * 8 if food_potato_unit == 5
replace food_potato_grams = food_potato_amt * 375 if food_potato_unit == 6
replace food_potato_grams = food_potato_amt * 200 if food_potato_unit == 8

/* Convert onions to grams */
gen food_onion_grams = food_onion_amt
replace food_onion_grams = food_onion_amt * 1000 if food_onion_unit == 1
replace food_onion_grams = . if food_onion_unit == 5
replace food_onion_grams = . if food_onion_unit == 6
replace food_onion_grams = food_onion_amt * 175 if food_onion_unit == 8

/* Convert green leafy vegetables to grams */
gen food_greenveg_grams = food_greenveg_amt
replace food_greenveg_grams = food_greenveg_amt * 1000 if food_greenveg_unit == 1
replace food_greenveg_grams = food_greenveg_amt * 750 if food_greenveg_unit == 8

/* Convert other vegetables to grams */
gen food_othveg_grams = food_othveg_amt
replace food_othveg_grams = food_othveg_amt * 1000 if food_othveg_unit == 1
replace food_othveg_grams = food_othveg_amt * 500 if food_othveg_unit == 8

/* Convert fruits/nuts to grams */
gen food_fruitsnuts_grams = food_fruitsnuts_amt
replace food_fruitsnuts_grams = food_fruitsnuts_amt * 1000 if food_fruitsnuts_unit == 1
replace food_fruitsnuts_grams = food_fruitsnuts_amt * 250 if food_fruitsnuts_unit == 8
replace food_fruitsnuts_grams = food_fruitsnuts_amt * 30 * 12 if food_fruitsnuts_unit == 9

/* Convert salt to grams
   1 mana = 500 grams
   As much as 4 kilos of salt, seems like maybe misplaced unit */
gen food_salt_grams = food_salt_amt
replace food_salt_grams = food_salt_amt * 1000 if food_salt_unit == 1
replace food_salt_grams = food_salt_amt * 1000 if food_salt_unit == 8
replace food_salt_grams = food_salt_amt * 5.92 * 500 / 545 if food_salt_unit == 10

/* Convert other spices to grams
   1 metric teaspoon = 2.13 grams (source: http://www.aqua-calc.com/calculate/food-volume-to-weight using curry powder) */
gen food_othspice_grams = food_othspice_amt
replace food_othspice_grams = food_othspice_amt * 2.13 if food_othspice_unit == 10


/* Convert sugar to grams
   1 ml = 0.845 grams (source: http://www.onlineconversion.com/weight_volume_cooking.htm) */
gen food_sugar_grams = food_sugar_amt
replace food_sugar_grams = food_sugar_amt * 1000 if food_sugar_unit == 1
replace food_sugar_grams = food_sugar_amt * 5.92 * 0.845 if food_sugar_unit == 10


/***************************************/
/* STEP 2: CONVERT GRAMS INTO CALORIES */
/***************************************/

/* This conversion follows Nicholas Li's conversion procedure for the NSS in India */
/* Convert rice to calories as follows:
   100g = 345 calories */
      
gen food_rice_cals = food_rice_grams*345/100

/* Convert beaten rice to calories as follows:
   100g = 346 calories  */
   
gen food_beatenrice_cals = food_beatenrice_grams*346/100

/* Convert maize to calories as follows:
   100g = 342 calories */
gen food_maize_cals = food_maize_grams*342/100

/* Convert wheat to calories as follows:
   100g = 290 cals */
gen food_wheat_cals = food_wheat_grams*290/100

/* Convert barley to calories as follows:
   100g = 336 cals */
gen food_barley_cals = food_barley_grams*336/100

/* Convert millet to calories as follows:
   100g = 331 cals */
gen food_millet_cals = food_millet_grams*331/100

/* Convert pulses to calories as follows:
   100g = 335 cals (arhar) */
gen food_pulses_cals = food_pulses_grams*346/100

/* Convert pea to calories as follows:
   100g = 360 cals (gram (whole)) */
gen food_pea_cals = food_pea_grams*346/100

/* Convert othbeans to calories as follows:
   100g = 353 cals (other pulses) */
gen food_othbeans_cals = food_othbeans_grams*353/100

/* Convert fish to calories
   100g = 219 cals */
gen food_fish_cals = food_fish_grams*219/100


/* Convert meat to calories
   100g = 109 cals (chicken, pork is 114, mutton is 156) */
gen food_meat_cals = food_meat_grams*109/100

/* Convert eggs to number
   1 egg = 173 cals */
gen food_egg_cals = food_egg_num*173

/* Convert milk to liters
   1 liter = 117 cals */
gen food_milk_cals = food_milk_liter*117

/* Convert curd to calories
   100g = 60 cals */
gen food_curd_cals = food_curd_grams*60/100

/* Convert powder milk to calories
   100g = 357 cals */
gen food_powdermilk_cals = food_powdermilk_grams*357/100

/* Convert ghee to calories  as follows:
   100g = 850 cals */
gen food_ghee_cals = food_ghee_grams*850/100

/* Convert honey to calories  as follows:
   100g = 319 cals */
gen food_honey_cals = food_honey_grams*319/100

/* Convert vegetable ghee to calories  as follows:
   100g = 850 cals */
gen food_vegghee_cals = food_vegghee_grams*850/100

/* Convert oil to calories
   100g = 900 cals */
gen food_oil_cals = food_oil_grams*900/100

/* Convert potato to calories
   100g = 97 cals */
gen food_potato_cals = food_potato_grams*97/100

/* Convert onions to calories
   100g = 50 cals */
gen food_onion_cals = food_onion_grams*50/100

/* Convert green leafy vegetables to calories
   100g = 26 cals (palak/other leafy vegetables) */
gen food_greenveg_cals = food_greenveg_grams*26/100

/* Convert other vegetables to calories
   100g = 30 cals (cauliflower) */
gen food_othveg_cals = food_othveg_grams*30/100

/* Convert fruits/nuts to calories
   100g = 74 cals (mango)
   nuts = 628 cals per 100g */
gen food_fruitsnuts_cals = food_fruitsnuts_grams*74/100

/* Convert salt to calories
   100g = .00001 cals */
gen food_salt_cals = food_salt_grams*.00001/100

/* Convert other spices to calories
   100g = 250 cals*/
gen food_othspice_cals = food_othspice_grams*250/100

/* Convert sugar to calories
   100g = 398 cals*/
gen food_sugar_cals = food_sugar_grams*346/100

local foods "rice beatenrice maize wheat barley millet pulses pea othbeans fish meat egg milk curd powdermilk ghee honey vegghee oil potato onion greenveg othveg fruitsnuts salt othspice sugar sakhar sweets tea alcohol nonalcohol tobacco noodles othfoods"
local foods_nonzero ""

foreach var of local foods {
gen `var'_nonzero = food_`var'_amt ~= 0
local foods_nonzero "`foods_nonzero' `var'_nonzero"
}

egen total_cals = rowtotal(*_cals)
gen cals_perperson = total_cals/num_hh_members_residing

gen cals_rice = food_rice_cals/total_cals
gen cals_pulses = food_pulses_cals/total_cals
gen cals_wheat = food_wheat_cals/total_cals
gen cals_oil = food_oil_cals/total_cals
gen cals_maize = food_maize_cals/total_cals
gen cals_potatoes = food_potato_cals/total_cals

save "`follow_up_raw_hh'", replace

/* Merge in information on Treatment/Control */
use "`folder'/Data/cosampling.dta", clear
keep if sampco == 1
keep district vdc control_vdc info_vdc cash_vdc
by district vdc, sort: keep if _n == 1
rename vdc vdc_name
gen district_n = 1 if district == "Ramechhap"
replace district_n = 2 if district == "Sindhuli"
replace district_n = 3 if district == "Sarlahi"
replace district_n = 4 if district == "Rautahat"
drop district
rename district_n district
merge 1:m district vdc_name using "`follow_up_raw_hh'", nogen
sort www hh sn

/* Generate one variable with all three treatment indicators, and label it,
   for the purpose of generating tables */
gen treatment = 1 if control_vdc == 1
replace treatment = 2 if info_vdc == 1
replace treatment = 3 if cash_vdc == 1

label define treatgroup 1 "Control" 2 "Info Only" 3 "Info + Cash"
label values treatment treatgroup

/* Generate the three comparison dummies so that we can do 2-way
   hypothesis testing */
gen cash_control = cash_vdc
replace cash_control = . if info_vdc == 1

gen info_control = info_vdc
replace info_control = . if cash_vdc == 1

gen cash_info = cash_vdc
replace cash_info = . if control_vdc == 1

save "`folder'/Data/follow_up_raw_hh", replace
